package com.sunny.ynhz.ui.base;

import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.just.library.AgentWeb;
import com.just.library.ChromeClientCallbackManager;
import com.sunny.ynhz.R;
import com.sunny.ynhz.utils.MD5Utils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by luao on 2017/8/30.
 */

public class BaseWebFragment extends BaseFragment {


    public
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    public AgentWeb mAgentWeb;
    private ChromeClientCallbackManager.ReceivedTitleCallback mCallback;
    private String url="";


    @Override
    protected void init() {
        super.init();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        ButterKnife.bind(activity);
        loadUrl(url);
        mCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {
            @Override
            public void onReceivedTitle(WebView view, String title) {

            }
        };
        if (url.indexOf("?") != -1) {
            url = url + "&token=" + MD5Utils.string2MD5((System.currentTimeMillis() * 5 - 1799) + "") + "&tokentime=" + System.currentTimeMillis();
        } else {
            url = url + "?token=" + MD5Utils.string2MD5((System.currentTimeMillis() * 5 - 1799) + "") + "&tokentime=" + System.currentTimeMillis();
        }
        mAgentWeb = AgentWeb.with(activity)//传入Activity
                .setAgentWebParent(linearLayout, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams ,第一个参数和第二个参数应该对应。
                .useDefaultIndicator()// 使用默认进度条
                .defaultProgressBarColor() // 使用默认进度条颜色
                .setReceivedTitleCallback(mCallback) //设置 Web 页面的 title 回调
                .createAgentWeb()//
                .ready()
                .go(url);
    }


    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_base_web;
    }


    public void loadUrl(String url) {
        this.url = url;
    }
}
